package com.upbos.upm.dept;

import com.upbos.core.ret.RetData;
import com.upbos.sso.UserContext;
import com.upbos.upm.common.Constants;
import com.upbos.upm.dict.DictUtil;
import com.upbos.upm.entity.Dept;
import com.upbos.upm.entity.DictMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

import static com.upbos.core.ret.RetData.success;

/**
 * <b>Application name：</b> DeptController.java <br>
 * <b>Application describing：部门管理 </b> <br>
 * <b>Copyright：</b> Copyright &copy; 2019 upbos.com 版权所有。<br>
 * <b>Company：</b> upbos.com <br>
 * <b>@Date：</b> 2019年11月08日 13:59 <br>
 * <b>@author：</b> <a href="mailto:Jason@miyzh.com"> Jason </a> <br>
 * <b>@version：</b>V5.0.0 <br>
 */
@RestController
@RequestMapping("upm/dept")
public class DeptController {

    /**
     * 部门管理服务类
     */
    @Resource
    private DeptService deptService;


    /**
     * 获取下级节点列表
     *
     * @param parentId 上级节点Id
     * @return 下级节点列表
     */
    @RequestMapping("listDeptByParentId")
    public RetData listDeptByParentId(String parentId) throws Exception {
        List<Dept> list = deptService.listDeptByParentId(parentId);
        // 翻译部门类型代码
        DictUtil.translate(list, new DictMapping("deptType", "type", "typeName"));
        return success(list);
    }

    /**
     * 根据名称模糊查询部门列表
     *
     * @param name 部门名称
     * @return 部门列表
     */
    @RequestMapping("listDept")
    public RetData listDept(String orgId, String name, UserContext userContext) throws Exception {
        if (!userContext.hasPrivilege(Constants.PRIVILEGE_ADMIN_KEY)) {
            orgId = userContext.getSsoUser().getOrgId();
        }

        List<Dept> list = deptService.listDept(orgId, name);
        DictUtil.translate(list, new DictMapping("deptType", "type", "typeName"));

        return success(list);
    }

    /**
     * 保存部门
     *
     * @param dept 部门
     */
    @RequestMapping("saveDept")
    public RetData saveDept(UserContext userContext, Dept dept) {
        return success(deptService.saveDept(dept, userContext));
    }

    /**
     * 删除部门
     *
     * @param dept 部门
     */
    @RequestMapping("deleteDept")
    public RetData deleteDept(Dept dept) {
        return deptService.deleteDept(dept);
    }
}
